﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ToppingControl.Models;
using WinNemoLib;
using ReportManagement;
using System.Web.Security;

namespace ToppingControl.Controllers
{
    public class TailLineController : Controller
    {
        //
        // GET: /TailLine/

       public ActionResult CheckProgecess()
       {
          List<TopCordActModel.TopCordAct_DS> list = new List<TopCordActModel.TopCordAct_DS>();
          TopCordActModel mdSearch = new TopCordActModel();
          list = mdSearch.GET_NOFINISH();
          if (list.Count > 0)
          {
              return RedirectToAction("GET_CARD", new { code = list[0]._Tcrdcd });
          }
          return RedirectToAction("Index");
       }

        public ActionResult Index()
        {
           ViewBag.shift = Session["shift"].ToString();
           List<TopCordActModel.TopCordAct_DS> list = new List<TopCordActModel.TopCordAct_DS>();
           TopCordActModel mdSearch = new TopCordActModel();
           list = mdSearch.SEL_ACT_FORTOP();
           return View(list);

        }

        public ActionResult Get_CARD(string msg="",string code="")
        {
           ViewBag.shift = Session["shift"].ToString();
           TopCordActModel.TopCordAct_DS list = new TopCordActModel.TopCordAct_DS();
           TopCordActModel mdSearch = new TopCordActModel();
            var obj= mdSearch.GET_ACT_FORTOP(" Top 1 ","","","","","","",code);
            if (obj.Count > 0)
            {
               list = obj[0];

               if (StrUtil.IsEmpty (list._Rollno))
               {
                  list._Rollno = mdSearch.GET_ROLL(list);
               
                 
               }  
                CompoundUseModel com = new CompoundUseModel();
                com.GET_CPD_CH(ref list);
            }
            list._msg = msg;
           return View(list);
        }

       

        public void Regis_User(string shift, string emp, string code)
        {

           FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(1, emp
               , DateTime.Now, DateTime.Now.AddMinutes(60), true, code);

           // Encrypt the cookie using the machine key for secure transport
           string hash = FormsAuthentication.Encrypt(ticket);
           HttpCookie cookie = new HttpCookie(
              FormsAuthentication.FormsCookieName, // Name of auth cookie
              hash); // Hashed ticket

           // Set the cookie's expiration time to the tickets expiration time
           if (ticket.IsPersistent) cookie.Expires = ticket.Expiration;

           Response.Cookies.Add
           (
               new HttpCookie
               (
                   FormsAuthentication.FormsCookieName,
                   FormsAuthentication.Encrypt(ticket)
               )
           );

        }
        public ActionResult Register(string shift, string emp, string code)
        {
           Session["shift"] = shift;
           Regis_User(shift, emp, code);
           return RedirectToAction("CheckProgecess");
        }

        public JsonResult Save_data(TopCordActModel.TopCordAct_DS thedata,Boolean alt=false)
        {
           TopCordActModel ins = new TopCordActModel();
           if (ins.INS_ACT(thedata))
           {
              return Json(new { success = 1, message = "บันทึกเรียบร้อย" });
           }
           else return Json(new { success = 0, message = "ไม่สามารถบันทึกได้" });
        }

        public JsonResult Finish(TopCordActModel.TopCordAct_DS thedata)
        {
           TopCordActModel ins = new TopCordActModel();
           thedata._Finflag = "X";
           if (ins.INS_ACT(thedata))
           {
              return Json(new { success = 1, message = "บันทึกเรียบร้อย" });
           }
           else return Json(new { success = 0, message = "ไม่สามารถบันทึกได้" });
        }

        public JsonResult NewRow(TopCordActModel.TopCordAct_DS thedata)
        {
           TopCordActModel ins = new TopCordActModel();
           thedata._Finflag = "X";
           if (!ins.INS_ACT(thedata))
           {
              return Json(new { success = 0, message = "ไม่สามารถบันทึกได้" });
           }

           TopCordActModel.TopCordAct_DS newData = new TopCordActModel.TopCordAct_DS();
           newData._Manufc = thedata._Manufc;
           newData._Topno = thedata._Topno;
           newData._Itemno = thedata._Itemno;
           newData._Plandt = thedata._Plandt;
           newData._Topcrdcd = thedata._Topcrdcd;
           newData._Tcrdcd = thedata._Tcrdcd;
           newData._Tcrdlot = thedata._Tcrdlot;
           newData._HLength = thedata._HLength;
           newData._HExp = thedata._HExp;
           newData._Compound = thedata._Compound;
           newData._ComLot = thedata._ComLot;
           newData._ComPlt = thedata._ComPlt;
           newData._Prddate =  DateUtil.ToInternalDate(DateTime.Now, WinNemoLib.DateUtil.eLanguage.EN);  
           newData._Rollno = (Convert.ToInt32(thedata._Rollno) + 1).ToString();
           if (!ins.INS_ACT(newData))
           {
              return Json(new { success = 0, message = "ไม่สามารถบันทึกได้" });
           }
           return Json(new { success = 1, message = "บันทึกเรียบร้อย" });

        }


       /*****************************##############    WASTE  #################********************************************/
        public ActionResult WASTE()
        {
           ViewBag.dataW = WinNemoLib.DateUtil.ToDisplayDate(DateTime.Now, WinNemoLib.DateUtil.eLanguage.EN);
           List<Top_Waste.TOP_WASTE_DS> list = new List<Top_Waste.TOP_WASTE_DS>();
           Top_Waste mdSearch = new Top_Waste();
           list = mdSearch.SEL_TOP_WAS(WinNemoLib.DateUtil.ToDisplayDate(DateTime.Now, WinNemoLib.DateUtil.eLanguage.EN));

           return View(list);
        }

        public JsonResult INS_WASTE_DATA(Top_Waste.TOP_WASTE_DS theData)
        {

           Top_Waste mdUpd = new Top_Waste();

           if (mdUpd.Ins_Data(theData))
           {
              return Json(new { success = 1, message = "บันทึกเรียบร้อย" });
           }
           else
           {
              return Json(new { success = 0, message = "ไม่สามารถบันทึกได้" });
           }

        }

 /*****************************##############    PRINT  #################********************************************/

        [HttpPost]
        public JsonResult Get_Next()
        {
            List<TopCordActModel.TopCordAct_DS> list = new List<TopCordActModel.TopCordAct_DS>();
            TopCordActModel mdSearch = new TopCordActModel();
            list = mdSearch.SEL_ACT_FORTOP();
            if (list.Count > 1)
            {
                return Json(new { success = 1, list = list[1] });
            }
            return Json(new { success = 0, message = "บันทึกเรียบร้อย" });
        }


        public ActionResult PRINT_BEfore(string topcode, string cust,string prddt, string tirelot, string rollno)
        {
           TopCordActModel.TopCordAct_DS thedata = new TopCordActModel.TopCordAct_DS();
           thedata._Topcrdcd = topcode;
           thedata._Manufc = cust;
           thedata._Tcrdlot = tirelot;
           thedata._Rollno = rollno;
           thedata._Prddate = prddt;
           return View(thedata);
        }

   

    }
}
